<?php

namespace app\common\helper\printer\feie;





class FeiePrinter{

    protected $IP='api.feieyun.cn';

    protected $PORT='80';

    protected $PATH='/Api/Open/';

    protected $config;



    public function __construct($config=[])

    {

        $this->config=$config;

    }



    public function addPrinter($print_sn,$print_key,$printer_name)

    {

        $printerContent = $print_sn."#".$print_key."#".$printer_name;

        $time = time();         //请求时间

        $msgInfo = array(

            'user'=>$this->config['user'],

            'stime'=>$time,

            'sig'=>$this->signature($time),

            'apiname'=>'Open_printerAddlist',

            'printerContent'=>$printerContent

        );

        $client = new HttpClient($this->IP,$this->PORT);

        if(!$client->post($this->PATH,$msgInfo)){

            $this->error('添加失败');

        }else{

            //服务器返回的JSON字符串，建议要当做日志记录起来

            $result = $client->getContent();

            $result=json_decode($result,true);

            if($result['ret'] !=0){

                $this->error($result['msg']);

            }

            if(!empty($res['data']['no'])){

                $this->error($res['data']['no'][0]);

            }

        }

    }



    public function printMsg($print_sn,$content)

    {

        $time = time();         //请求时间

        $msgInfo = array(

            'user'=>$this->config['user'],

            'stime'=>$time,

            'sig'=>$this->signature($time),

            'apiname'=>'Open_printMsg',

            'sn'=>$print_sn,

            'content'=>$content,

            'times'=>1//打印次数

        );

        $client = new HttpClient($this->IP,$this->PORT);

        if(!$client->post($this->PATH,$msgInfo)){

            return  'error';

        }else{

            //服务器返回的JSON字符串，建议要当做日志记录起来

            $result = $client->getContent();

            return  $result;

        }

    }







    public function signature($time){

        return sha1( $this->config['user'].$this->config['ukey'].$time);//公共参数，请求公钥

    }



    public function error($message){

        throw new \Exception($message);

    }



}

